package cn.yudao.ymcc.web.controller;

import cn.yudao.ymcc.dto.CourseSaveDto;
import cn.yudao.ymcc.service.ICourseService;
import cn.yudao.ymcc.domain.Course;
import cn.yudao.ymcc.query.CourseQuery;
import cn.yudao.ymcc.result.JSONResult;
import cn.yudao.ymcc.result.PageList;
import cn.yudao.ymcc.vo.CourseDetailInfoVo;
import cn.yudao.ymcc.vo.CourseInfoVo;
import cn.yudao.ymcc.vo.RecommendedCourse;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    public ICourseService courseService;

    /**
     * 课程上架
     */
    @RequestMapping(value="/onLineCourse/{courseId}",method= RequestMethod.POST)
    @PreAuthorize("hasAuthority('course:online')")
    public JSONResult onLineCourse(@PathVariable("courseId") Long courseId){

        courseService.onLineCourse(courseId);

        return JSONResult.success();
    }

    /**
    * @Title: recommendedCourse
    * @Description: 课程推荐
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/9/7 18:46
    * @Parameters:
    * @Return
    */
    @RequestMapping(value="/recommendedcourse/{courseId}",method= RequestMethod.POST)
    public JSONResult recommendedCourse(@PathVariable("courseId") @Valid @NotNull(message = "请选择课程") Long courseId){

        courseService.recommendedCourse(courseId);

        return JSONResult.success();
    }


    /**
    * @Title: getRecommendedCourses
    * @Description: 获取推荐课程
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/9/7 19:27
    * @Parameters:
    * @Return
    */
    @RequestMapping(value="/getrecommendedcourses",method= RequestMethod.GET)
    public JSONResult getRecommendedCourses(){

        List<RecommendedCourse> recommendedCourses =  courseService.getRecommendedCourses();

        return JSONResult.success(recommendedCourses);
    }

    /**
    * @Title: detail
    * @Description: 课程详情页
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/8/18 13:03
    * @Parameters:
    * @Return
    */
    @RequestMapping(value="/detail/data/{courseId}",method= RequestMethod.GET)
    public JSONResult detail(@PathVariable("courseId") Long courseId){

        CourseDetailInfoVo vo = courseService.detail(courseId);

        return JSONResult.success(vo);
    }

    /**
    * @Title: offLineCourse
    * @Description: 课程下架
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/8/15 10:02
    * @Parameters:
    * @Return
    */
    @RequestMapping(value="/offLineCourse/{courseId}",method= RequestMethod.POST)
    public JSONResult offLineCourse(@PathVariable("courseId") Long courseId){

        courseService.offLineCourse(courseId);

        return JSONResult.success();
    }



    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult save(@RequestBody CourseSaveDto dto){

        courseService.insert(dto);

        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        courseService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(courseService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(courseService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('course:pagelist')")
    public JSONResult page(@RequestBody CourseQuery query){
        Page<Course> page = new Page<Course>(query.getPage(),query.getRows());
        Wrapper<Course> Wrapper = new EntityWrapper<>();
        Wrapper.like("name",query.getKeyword());
        page = courseService.selectPage(page,Wrapper);
        return JSONResult.success(new PageList<Course>(page.getTotal(),page.getRecords()));
    }


    /**
    * @Title: getForUser
    * @Description: 根据mediaId查询到当前登录用户是否拥有这个课程的媒体资源数据
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/8/17 15:00
    * @Parameters:
    * @Return
    */
    @RequestMapping(value = "/getForUser/{mediaId}",method = RequestMethod.GET)
    public JSONResult getForUser(@PathVariable("mediaId")Long mediaId){
        String fileUrl = courseService.getForUser(mediaId);
        return JSONResult.success(fileUrl);
    }

    /**
    * @Title: info
    * @Description: 获取订单页数据
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/8/17 18:02
    * @Parameters:
    * @Return
    */
    @RequestMapping(value = "/info/{courseIds}",method = RequestMethod.GET)
    public JSONResult courseOrderInfo(@PathVariable("courseIds")String courseIds){
        CourseInfoVo courseOrderInfo = courseService.courseOrderInfo(courseIds);
        return JSONResult.success(courseOrderInfo);
    }


    /**
    * @Title: getCourseByIds
    * @Description: 根据ids获取课程和课程销售
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/8/18 13:20
    * @Parameters:
    * @Return
    */
    @RequestMapping(value = "/getcoursebyids",method = RequestMethod.POST)
    public JSONResult getCourseByIds(@RequestBody List<Long> ids){
        CourseInfoVo vo = courseService.getCourseByIds(ids);
        return JSONResult.success(JSON.toJSONString(vo));
    }

}
